<?php
require_once 'Db/Db_Db.php';
class Application_Model_Server {
	
	public function getQuestions($num = null) {
		
		if (!isset ( $num ))
			$get_num = 10;
		else if(is_array($num))
			$get_num = $num[0];
			
		//Create COnnect Db
		$db = new Db_Db ( );
		$get_result = array();
		try{
			$conn = $db->conn_db();
			$query ="SELECT * FROM questions LIMIT 0 , 10;";
			$result = mysqli_query($conn,$query) or die(mysqli_error($conn));

			if(mysqli_num_rows($result) == 0){
					return array('name'=>"Data empty");
			}
			else{
				//var_dump($result);
				$set_questions = array();
				$i =0;
				while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
						$set_questions[$i]['id']				=  $row['id'];
						$set_questions[$i]['name']			=  $row['name'];
						$set_questions[$i]['question_a']	=  $row['question_a'];
						$set_questions[$i]['question_b']	=  $row['question_b'];
						$set_questions[$i]['question_c']	=  $row['question_c'];
						$set_questions[$i]['question_d']	=  $row['question_d'];
						$set_questions[$i]['answer']			=  $row['answer'];
						$i++;
				}
			}

		}
		catch(Zend_Db_Exception $e){
		      echo 'Error: '.$e->getMessage();
		   }
		return $set_questions;
	}
	
	/**
	 * get 5 easy question
	 */
	public function getQuestionLevelData($level = NULL){
		
			$limit_question = 5;

		//Create COnnect Db
		$db = new Db_Db ( );
		$get_result = array();
		
		try{
			$conn = $db->conn_db();
			$query ="SELECT * FROM questions WHERE 'level_id' =$level LIMIT 0 , $limit_question";
			var_dump($query);
			die();
			$result = mysqli_query($conn,$query) or die(mysqli_error($conn));

			if(mysqli_num_rows($result) == 0){
					return array('name'=>"Data empty");
			}
			else{
				//var_dump($result);
				$set_questions = array();
				$i =0;
				while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
						$set_questions[$i]['id']			=  $row['id'];
						$set_questions[$i]['level_id']		=  $row['level_id'];
						$set_questions[$i]['name']			=  $row['name'];
						$set_questions[$i]['question_a']	=  $row['question_a'];
						$set_questions[$i]['question_b']	=  $row['question_b'];
						$set_questions[$i]['question_c']	=  $row['question_c'];
						$set_questions[$i]['question_d']	=  $row['question_d'];
						$set_questions[$i]['answer']		=  $row['answer'];
						$i++;
				}
			}

		}
		catch(Zend_Db_Exception $e){
		      echo 'Error: '.$e->getMessage();
		   }
		return $set_questions;
	}
	//-----------------------------------------------------------

	/**
	 * Fetches data 
	 * 
	 * @param integer $num
	 * @return array
	 * cf.getData
	 */
	public function getData($num = null) {
		
		if (! isset ( $num ))
			$num = 10;
		$data = array ();
		
		for($a = 0; $a < $num; $a ++) {
			$data [] = array ('title' => 'Codeforest.net', 'number' => $a + 1, 'datetime' => date ( 'Y-m-d H:i:s' ) );
		}
		
		return $data;
	}
	
	//-----------------------------------------------------------
	/**
	 * Get Quesstion method
	 * 
	 * @return Array
	 */
	public function questionData($num) {
		
		if (!isset ( $num ))
			$num = 10;
		else if(is_array($num))
			$get_num = $num[0];
		$data_qs = array ();
		for($a = 0; $a < $get_num; $a ++) {
			$rand = mt_rand ( 2, 5 );
			$rand = $rand - 1;
			$data_qs [] = array (
				'name' => 'These days everbody is aware .......... the danger of smoking. ', 
				'question_a' => '1',
				 'question_b' => '2', 
				 'question_c' => '3', 
				 'question_d' => '4', 
				 'answer' => '' . $rand . '', 
				 'created' => date ( 'Y-m-d H:i:s' ),
				 'modified' => date ( 'Y-m-d H:i:s' ) 
			);
		}
		return $data_qs;
	}

	//-----------------------------------------------------------
	/**
	 * Test method
	 * 
	 * @return string
	 */
	public function test() {
		return 'Hello XMLRPC!';
	}
}